package com.zkk.qqclient.service;

import com.zkk.qqcommon.Message;
import com.zkk.qqcommon.MessageType;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 * 该类提供和消息相关的服务方法
 * */
public class MessageClientService {

    //私聊
    public void sendMessageToOne(String senderId,String content,String getterId){
        //构建Message对象
        Message message = new Message();
        message.setMesType(MessageType.MESSAGE_COMM_MES);
        message.setSender(senderId);
        message.setContent(content);
        message.setGetter(getterId);
        message.setSendTime(new Date().toString());
        //发送给服务器
        try {
            ObjectOutputStream oos = new ObjectOutputStream(ManageClientConnectServerThread.getClientConnectServerThread(senderId).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //群发
    public void sendMessageToAll(String senderId,String content){
        //构建Message
        Message message = new Message();
        message.setSender(senderId);
        message.setMesType(MessageType.MESSAGE_TO_ALL);
        message.setContent(content);
        //发送到服务器
        try {
            ObjectOutputStream oos =
                    new ObjectOutputStream(ManageClientConnectServerThread.getClientConnectServerThread(senderId).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
